package com.example.health_life.service;

import com.example.health_life.dao.New;
import com.example.health_life.dao.NewRepository;
import com.example.health_life.dto.UsersDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class NewServiceImpl implements NewService {

    @Autowired
    private NewRepository newRepository;

    @Autowired
    private UserService userService;

    @Override
    public List<New> getNewsList() {
        return newRepository.findAll();
    }

    @Override
    public List<New> getArticleListByType(Byte type) {
        return newRepository.findByType(type);
    }

    @Override
    public New getNewById(Integer id) {
        return newRepository.findById(id).get();
    }

    @Override
    public int addNew(New news) {
        // 设置默认值
        if (news.getFavorite() == null) {
            news.setFavorite(0);
        }
        if (news.getView() == null) {
            news.setView(0);
        }
        //将创建时间设置为当前时间
        news.setPublishTime(java.time.Instant.now());
        news.setStatus((byte) 1);
        return newRepository.save(news).getId();
    }

    @Override
    public int deleteNew(Integer id) {
        //判断新闻是否存在
        newRepository.findById(id).orElseThrow(() -> new IllegalStateException("新闻不存在"));

        // 存在，则将其status设置相反数
        int status = newRepository.findById(id).get().getStatus();
        if (status == 1) {
            status = -1;
        } else {
            status = 1;
        }
        Optional<New> news = newRepository.findById(id);
        news.get().setStatus((byte) status);
        newRepository.save(news.get());//保存改变的新闻
        return id;
    }

    @Override
    public int updateNew(Integer id, String title, String content, Integer type, String coverImg,
                        Integer view, Integer favorite, Integer status) {
        //判断新闻是否存在
        newRepository.findById(id).orElseThrow(() -> new IllegalStateException("新闻不存在"));

        Optional<New> news = newRepository.findById(id);

        if (type == 1 || type == 2 ) {
            int type1 = type;
            news.get().setType((byte) type1);
        }

        if (status == 1) {
            news.get().setStatus((byte) 1);
        }
        if (status == -1) {
            news.get().setStatus((byte) -1);
        }

        if (view != -9) {
            news.get().setView(view);
        }

        if(title!=null){
            news.get().setTitle(title);
        }

        news.get().setContent(content);
        news.get().setCoverImg(coverImg);

        if (favorite != -9) {
            news.get().setFavorite(favorite);
        }

        newRepository.save(news.get());
        return id;
    }

    @Override
    public List<New> getNewsListWithAuthor() {
        return getNewsListWithAuthor(1);
    }

    @Override
    public List<New> getNewsListWithAuthor(Integer status) {
        List<New> newsList;
        if (status == 0) {
            newsList = newRepository.findAll();
        } else {
            newsList = newRepository.findByStatus((byte) status.intValue());
        }
        for (New news : newsList) {
            UsersDTO author = userService.getUserById(news.getAuthorId());
            if (author != null) {
                news.setAuthorNickname(author.getNickname());
            }
        }
        return newsList;
    }

    @Override
    public New getNewByIdWithAuthor(Integer id) {
        New news = newRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("新闻不存在"));
        UsersDTO author = userService.getUserById(news.getAuthorId());
        if (author != null) {
            news.setAuthorNickname(author.getNickname());
        }
        return news;
    }

    @Override
    public List<New> getArticleListByTypeWithAuthor(Byte type) {
        return getArticleListByTypeWithAuthor(type, 1);
    }

    @Override
    public List<New> getArticleListByTypeWithAuthor(Byte type, Integer status) {
        List<New> newsList;
        if (status == 0) {
            newsList = newRepository.findByType(type);
        } else {
            newsList = newRepository.findByTypeAndStatus(type, (byte) status.intValue());
        }
        for (New news : newsList) {
            UsersDTO author = userService.getUserById(news.getAuthorId());
            if (author != null) {
                news.setAuthorNickname(author.getNickname());
            }
        }
        return newsList;
    }
}
